[MySQL] Efficiently store last X records per item

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-04-06T08:49:58Z Indexed on 2010/04/06 9:23 UTC
Read the original article Hit count: 259

Filed under:
|
|

I want to store the last X records in an MySQL database in an efficient way. So when the 4th record is stored the first should be deleted.

The way I do this not is first run a query getting the items. Than check what I should do then insert/delete.

There has to be a better way to do this. Any suggestions?

Edit

I think I should add that the records stored do not have a unique number. They have a mixed par. For example article_id and user_id.

Then I want to make a table with the last X items for user_x.

Just selecting the article from the table grouped by user and sorted by time is not an option for me. The table where I do the sort and group on has millions of records and gets hit a lot for no reason. So making a table in between with the last X records is way more effient.

PS. I am not using this for articles and users.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about efficiency